package com.icloud.cer.rest.msg.filter;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExecutorThreadFactory implements ThreadFactory {
	
	private static final Logger log = LoggerFactory.getLogger(ExecutorThreadFactory.class);
	
	static final AtomicInteger poolNumber = new AtomicInteger(1);
	private final ThreadGroup group;
	private final AtomicInteger threadNumber = new AtomicInteger(1);
	private final String namePrefix;
	
	public ExecutorThreadFactory(String name) {
		SecurityManager s = System.getSecurityManager();
		group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
		namePrefix = name + "-thread-";
	}
	
	public ExecutorThreadFactory() {
		SecurityManager s = System.getSecurityManager();
		group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
		namePrefix = "Pool-thread-";
	}
	

	@Override
	public Thread newThread(Runnable run) {
		Thread td = new Thread(group, run, namePrefix + threadNumber.getAndIncrement(), 0);
		log.info("thread name:" + td.getName());
		if (td.isDaemon()) {
			td.setDaemon(false);
		}
		// 优先级
		td.setPriority(Thread.NORM_PRIORITY + 1);

		return td;
		
	}

}
